﻿#include "orvsettings.h"
#include "ui_orvsettings.h"
#include <QSettings>
#include <QMessageBox>

orvSettings::orvSettings(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::orvSettings)
{
    ui->setupUi(this);
    readIni();
    createConnection();
    setStrings();

    model = new QSqlTableModel;
    model->setTable("stafflist");
    model->setEditStrategy(QSqlTableModel::QSqlTableModel::OnFieldChange);
    model->select();
    ui->stuffList->setModel(model);
    ui->stuffList->setModelColumn(0);
    ui->stuffList->show();

}

orvSettings::~orvSettings()
{
    db.close();
    delete model;
    delete ui;
}

void orvSettings::readIni()
{
    QSettings *settings=new QSettings(QApplication::applicationDirPath()+"\\orvbase.ini",QSettings::IniFormat);
    dbHost=settings->value("dbhost").toString();
    dbName=settings->value("dbname").toString();
    dbUser=settings->value("dbuser").toString();
    dbPassword=settings->value("dbpassword").toString();

    QByteArray xcode;
    xcode.append(dbPassword);
    QByteArray precode(QByteArray::fromBase64(xcode));
    dbPassword = precode.data();
    delete settings;
}

void orvSettings::writeIni()
{
    QSettings *settings=new QSettings(QApplication::applicationDirPath()+"\\orvbase.ini",QSettings::IniFormat);
    settings->setValue("dbhost",dbHost);
    settings->setValue("dbname",dbName);
    settings->setValue("dbuser",dbUser);
    QByteArray pass;
    pass.append(dbPassword);
    settings->setValue("dbpassword",QString(pass.toBase64()));
    delete settings;
}

void orvSettings::setStrings()
{
    ui->dbHostEdit->setText(dbHost);
    ui->dbNameEdit->setText(dbName);
    ui->dbUserEdit->setText(dbUser);
    ui->dbPasswordEdit->setText(dbPassword);
}

void orvSettings::getStrings()
{
    dbHost=ui->dbHostEdit->text();
    dbName=ui->dbNameEdit->text();
    dbUser=ui->dbUserEdit->text();
    dbPassword=ui->dbPasswordEdit->text();
}

void orvSettings::on_okButton_clicked()
{
    getStrings();
    writeIni();
    if(db.isOpen())db.close();
    db.setHostName(getHost());
    db.setDatabaseName(getName());
    db.setUserName(getUser());
    db.setPassword(getPassword());
    if(!db.open()){
        QMessageBox::warning(0,"Ошибка открытия базы данных",db.lastError().text());
        return;
    }
    emit settingsChanged();
}

void orvSettings::on_cancelButton_clicked()
{
    setStrings();
}

void orvSettings::createConnection()
{
    db = QSqlDatabase::addDatabase("QPSQL");
    db.setHostName(getHost());
    db.setDatabaseName(getName());
    db.setUserName(getUser());
    db.setPassword(getPassword());
    if(!db.open())QMessageBox::warning(0,"Ошибка открытия базы данных",db.lastError().text());

}

void orvSettings::on_addButton_clicked()
{
    QSqlRecord record;

    QSqlField f("who", QVariant::String);

    f.setValue(QVariant("Новый сотрудник"));
    record.append(f);
    model->insertRecord(-1,record)    ;
}

void orvSettings::on_delButton_clicked()
{
    if(ui->stuffList->currentIndex().row()<0)return;
    model->removeRows(ui->stuffList->currentIndex().row(),1);
}
